function showDialog(){
    //console.log("打开对话框");
    wx.showToast({
         title: '加载中',
         icon: 'loading',
         mask:'true',
      });
}
function showToast(word){
  wx.showToast({
    title: word,
    type:'info',
    mask: 'true',
  });
  setTimeout(function(){
    wx.hideToast();
  },800);
}
function closeDialog(){
    //console.log("关闭对话框");
    wx.hideToast();
}
function UrlLoad(address,params,loadOk){
    wx.request({
        url: address, //仅为示例，并非真实的接口地址
        data: params,
        method:'GET'
        ,
        header: {
            'content-type': 'application/json'
        }, 
        success: function(res) {
          loadOk(true,res);
        },
        fail:function(res){
          loadOk(false,res);
        }
    })
}
//加载筛选网络数据
function loadBasicDatas(loadOk){
    showDialog();
    UrlLoad('https://www.teachguide.net/index.php?m=Apicloud&c=index&a=basicQue',{},
        function(isOk,data){
            if(isOk){
                loadOk(true,data);
                closeDialog();
            }else{
                loadOk(false,data);
                closeDialog();
            }
        }
    )
}
//加载基础数据并把筛选数据上传
function loadBaseData(param,openid,group,loadOk){
    showDialog();
    UrlLoad('https://www.teachguide.net/index.php/Apicloud/index/leftQue',
    {"sub":param,
     "openid":openid,
     "group":group   
    },
        function(isOk,data){
            if(isOk){
                loadOk(true,data);
                closeDialog();
            }else{
                loadOk(false,data);
                closeDialog();
            }
        }
    )
}
//加载标签题
function loadLabelData(param,openid,group,loadOk){
    showDialog();
    UrlLoad('https://www.teachguide.net/index.php/Apicloud/index/labelQue',
    {"sub":param,
     "openid":openid,
     "group":group   
    },
        function(isOk,data){
            if(isOk){
                loadOk(true,data);
                closeDialog();
            }else{
                loadOk(false,data);
                closeDialog();
            }
        }
    )
}
//一般题目
function loadCommonData(param,openid,group,loadOk){
    showDialog();
    UrlLoad('https://www.teachguide.net/index.php/Apicloud/index/commonlyQue',
    {"sub":param,
    "openid":openid,
    "group":group   
    },
        function(isOk,data){
            if(isOk){
                loadOk(true,data);
                closeDialog();
            }else{
                loadOk(false,data);
                closeDialog();
            }
        }
    )
}
//加载最弱学课
function loadWeak(param,openid,group,loadOk){
    showDialog();
    UrlLoad('https://www.teachguide.net/index.php/Apicloud/index/weak',
    {"sub":param,
     "openid":openid,
     "group":group   
    },
        function(isOk,data){
            if(isOk){
                loadOk(true,data);
                closeDialog();
            }else{
                loadOk(false,data);
                closeDialog();
            }
        }
    )
}
//https://api.weixin.qq.com/sns/jscode2session
function userLogin(cb){
    wx.login({
        success:function(e){
            console.log("登录成功");
            console.log(e);
            wx.request({
              url: 'https://www.teachguide.net/index.php/Apicloud/index/getOpenid',
              data: {
                  "js_code":e.code,
                  "appid":'wx7406984030fd2297',
                  "secret":"d0268b610fd7cc2b72e4b859ce2fff19",
                  "grant_type":"authorization_code"
              },
              method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
              // header: {}, // 设置请求的 header
              success: function(res){
                // success
                console.log("login ok");
                console.log(res.data);
                cb(JSON.parse(res.data.result).openid,JSON.parse(res.data.result).session_key);
              },
              fail: function(res) {
                // fail
                console.log("login fail");
                console.log(res);
                cb('wx956321');
              },
              complete: function(res) {
                // complete
              }
            })
        },
        fail:function(e){
            console.log("登录失败");
            console.log(e);
        }
    });
}
function getMyUnionid(sessionKey,cb){
    var param = {
        'appid':'wx7406984030fd2297',
        'sessionKey':sessionKey,
        'encryptedData':'',
        'iv':''
    }
      //调用登录接口
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              console.log(res);
              param.encryptedData = res.encryptedData;
              param.iv = res.iv;
              UrlLoad('https://www.teachguide.net/index.php/Apicloud/WeiXin/getData',
                    param,
                        function(isOk,data){
                                // console.log('get my union id');
                                // getUnId(data.data);
                                // console.log(data.data);
                                // console.log(getUnId(data.data));
                                cb(getUnId(data.data));
                        });
            }
          })
        }
      })
}
function getUnId(msg){
    var strs = 'wx123456';
    try{
        strs = msg.split(',')[2].split(':')[1].split('"')[1];
    }catch (e){

    }
    return strs;
}
module.exports.showDialog = showDialog;
module.exports.closeDialog = closeDialog;
module.exports.loadBasicDatas = loadBasicDatas;
module.exports.loadBaseData = loadBaseData;
module.exports.loadLabelData = loadLabelData;
module.exports.loadCommonData  = loadCommonData; 
module.exports.loadWeak = loadWeak;
module.exports.userLogin = userLogin;
module.exports.getMyUnionid = getMyUnionid;
module.exports.showToast = showToast;